package com.atguigu.gmall.cart.service;

import com.atguigu.gmall.cart.mapper.CartMapper;
import com.atguigu.gmall.cart.pojo.Cart;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @description:
 * @author: xie.zh
 * @create: 2021.03.30   21:17
 * @version: 1.0
 */
@Service
public class CartAsyncService {

    @Autowired
    private CartMapper cartMapper;

    /**
     * 新增购物车
     *
     * @param cart
     */
    @Async
    public void insertCart(String userId, Cart cart) {
        this.cartMapper.insert(cart);
    }

    /**
     * 更新购物车
     *
     * @param cart
     * @param skuIdString
     * @param userId
     */
    @Async
    public void updateCart(String userId, Cart cart, String skuIdString) {
        int i = 1 / 0;
        this.cartMapper.update(cart, new UpdateWrapper<Cart>().eq("sku_id", skuIdString).eq("user_id", userId));
    }

    public void deleteCartByUserId(String userId) {
        this.cartMapper.delete(new UpdateWrapper<Cart>().eq("user_id", userId));
    }

    public void deleteCartByUserIdAndSkuId(String userId, Long skuId) {
        this.cartMapper.delete(new UpdateWrapper<Cart>().eq("user_id", userId).eq("sku_id", skuId));
    }
}
